#include "Controller.h"

Controller::Controller(EventQueue<LogType>* log_queue){
  p = EventProcess::GetInstance(log_queue);
  p->Start();
	cout<<"lzf start ok"<<endl;
}

Controller::~Controller(){
  p->Join();
  delete p;
}
int Controller::SetProcess(int set){
  set_ = set;
  p->AddEvents_setting(set);
  return 0;
}

int Controller::PlayProcess(){
  //Decoder()
 // p->AddEvents(1,ID);
 
  xmldecoder();
  cout << "this is PLAYPROCESS!" << endl;
  return 0;
}

int Controller::SetIP(struct IPset ip){
   FILE *fp = fopen(NETWORK,"w+");
   string lo = "auto lo\niface lo inet loopback\n\nauto eth0\
                \niface eth0 inet static\
                \n    address "  +ip.address+\
               "\n    netmask "  +ip.netmask+\
               "\n    network "  +ip.network+\
               "\n    broadcast "+ip.broadcast;
   fwrite(lo.c_str(),sizeof(char),lo.size(),fp);
   system("/etc/init.d/networking restart");
   cout << lo <<endl;
   fclose(fp);

}

int Controller::SetTime(string time){
    string timeset = "date -s " + time + "&&hwclock --systohc";
    cout << timeset <<endl;
    int a = system(timeset.c_str()) ;
    if(a ==0){
      cout << "set time sucess!"<<endl;
      return 0;
    }
    else{
      cerr << "set time false! "<<endl;
      return -1;
    }
}

int Controller::DeleteFile(){
    xmlDocPtr doc;           
    xmlNodePtr curNode;      
    xmlChar *szKey;        
    const char *szDocName = "./program/1.xml";
    int N=0;
    int i=0;
    doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); 
    if (NULL == doc) 
    {  
       fprintf(stderr,"Document not parsed successfully. \n");     
       return -1; 
    } 
    curNode = xmlDocGetRootElement(doc); 
    if (NULL == curNode)
    { 
       fprintf(stderr,"empty document\n"); 
       xmlFreeDoc(doc); 
       return -1; 
    } 
    if (xmlStrcmp(curNode->name, BAD_CAST "root")) 
    {
       fprintf(stderr,"document of the wrong type, root node != root"); 
       xmlFreeDoc(doc); 
       return -1; 
    } 
    curNode = curNode->xmlChildrenNode;
    szKey=xmlNodeGetContent(curNode);
    string mode=reinterpret_cast<const char *>(szKey);
    if( mode == "INSERT" ) {
      cout <<"INSERT MODE"<<endl;
    }
    else if( mode =="NORMAL") {
      string location = "find ./program/* ! -name 1.xml -exec rm {} \\;";
      int a =system(location.c_str()) ;
      cout << "SYSTEM RM ACK = "<<a<<endl;
      cout <<"NORMAL MODE delete file now check"<<endl;
    }
    xmlFreeDoc(doc);
  return 0; 

}
int Controller::xmldecoder()
{

    xmlDocPtr doc;           
    xmlNodePtr curNode;      
    xmlChar *szKey;        
    const char *szDocName = "./program/1.xml";
    int N=0;
    int i=0;
    doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); 
    if (NULL == doc) 
    {  
       fprintf(stderr,"Document not parsed successfully. \n");     
       return -1; 
    } 
    curNode = xmlDocGetRootElement(doc); 
    if (NULL == curNode)
    { 
       fprintf(stderr,"empty document\n"); 
       xmlFreeDoc(doc); 
       return -1; 
    } 
    if (xmlStrcmp(curNode->name, BAD_CAST "root")) 
    {
       fprintf(stderr,"document of the wrong type, root node != root"); 
       xmlFreeDoc(doc); 
       return -1; 
    } 
    curNode = curNode->xmlChildrenNode;
    curNode = curNode->next;
    szKey=xmlNodeGetContent(curNode);
    N=atoi(reinterpret_cast<const char *>(szKey));
    play_data *data=new play_data[N];
    while(curNode->next!= NULL) 
    {
       curNode = curNode->next;
      if (xmlHasProp(curNode,BAD_CAST "mediatype"))
       {
           data[i].mediatype=atoi(reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "mediatype")));
       }

      if (xmlHasProp(curNode,BAD_CAST "location"))
       {
           data[i].location=reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "location"));
       }

      if (xmlHasProp(curNode,BAD_CAST "playtime"))
       {
           data[i].playtime=atoi(reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "playtime")));
       }
      if (xmlHasProp(curNode,BAD_CAST "insertmode"))
       {
           data[i].insertmode=atoi(reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "insertmode")));
       }

       i++; 
    } 

  for(i=0;i<N;i++)
  {  
    p->AddEvents_playing(data[i]);
  }
  xmlFreeDoc(doc);
  return 0; 
}
